﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CTTool.Web.Models
{
    public class RequestParam<T> where T : class
    {
        private string m_jsonData;
        public string jsonData
        {
            get
            {
                return m_jsonData;
            }
            set
            {
                m_jsonData = value;
                if (typeof(T) == typeof(List<JObject>))
                {
                    if (value == null)
                    {
                        this.Data = default(T);
                    }
                    else
                    {
                        if (value.Trim().StartsWith("[") && value.Trim().EndsWith("]"))
                        {
                            this.Data = JsonConvert.DeserializeObject<T>(value);
                        }
                        else
                        {
                            List<JObject> temp = new List<JObject>();
                            temp.Add(JsonConvert.DeserializeObject<JObject>(value));
                            this.Data = temp as T;
                        }
                    }
                }
                else
                {
                    this.Data = JsonConvert.DeserializeObject<T>(value);
                }
                
            }
        }
        public T Data { get; set; }
    }
}